<template>
  <div class="info_config_page">
    <div class="flex align-center table-top-btns">
      <h5 class="flex align-center">
        <svg-icon style="margin-right:15px" icon-class="list" :size="15" />数据列表
      </h5>
      <el-button size="small" icon="el-icon-plus" class="add-btn" @click="addBrand">新增品牌</el-button>
    </div>
    <el-table :data="tableData" row-key="id" border>
      <el-table-column align="center" label="ID" width="150" prop="id" />
      <el-table-column align="center" label="品牌名称">
        <template slot-scope="scope">
          <p><span v-if="!scope.row.children" style="margin:0 5px 0 20px;"></span>{{ scope.row.name }}</p>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="180">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'success' : 'danger'">
            {{ scope.row.status == 1 ? '显示' : '隐藏' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="scope.row.children && $hasMethod('#enable')" size="mini" type="success"
            @click="addItemChild(scope.row)">添加子菜单</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="warning" @click="handleEnable(scope.row.id, scope.row.status)">{{ scope.row.enabled
            == 1 ? '隐藏' : '显示' }}</el-button>
          <el-button size="mini" type="danger" @click="removeItem(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <custom-page ref="customPage" :total="total" @getList="getList" />
    <add-dialog ref="addDialog" :edit-item="editItem" @getList="getList"></add-dialog>
  </div>
</template>

<script>
import customPage from '@/components/customPage'
import addDialog from './components/addDialog'
import { getBrandList, updateBrandDown, deleteBrand, updateBrandUp } from '@/api/goods'
export default {
  components: { customPage, addDialog },
  data() {
    return {
      tableData: [],
      dialogTitle: '',
      total: 0,
      editItem:{}
    }
  },
  methods: {
    // 编辑
    handleEdit(item){
      this.editItem=item
      this.$refs.addDialog.dialogVisible = true
      this.$refs.addDialog.title = '编辑品牌'
    },
    // 显示隐藏
    handleEnable(id, enabled) {
      console.log(enabled);
      if (enabled == 1) {
        // 隐藏
        updateBrandDown(id).then(res => {
          this.getList()
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        })
      } else {
        // 显示
        updateBrandUp(id).then(res => {
          this.getList()
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        })
      }
    },
    // 删除
    removeItem(id) {
      this.$confirm('您确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteBrand(id).then(res => {
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
          this.getList()
        })
      }).catch(() => { })
    },
    addBrand() {
      this.$refs.addDialog.dialogVisible = true
      this.$refs.addDialog.title = '添加品牌'
    },
    // 获取列表
    getList() {
      const { page, pageSize } = this.$refs.customPage
      const params = {
        name: '',
        brandCreateTime: '',
        page,
        pageSize
      }
      getBrandList(params).then(res => {
        this.tableData = res.data
        this.total = res.total
      })
    }
  },
  mounted() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped></style>
